﻿using Dapper;
using Himp.TaskScheduling.Hangfire;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Options;
using System.Data;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 
    /// </summary>
    public class ManualStandardTestTaskWorker : IHostedService
    {
        #region SQL

        private ILogger<ManualStandardTestTaskWorker> _logger;
        //获取化验结果
        public const string GET_SAMPLE_PREPARATION_RESULT_SQL = @"
            SELECT *
            FROM [TaskScheduling].[TS_SamplePreparationResult]
            WHERE FORMAT(Begtime, 'yyyy-MM-dd') = FORMAT(GETDATE(), 'yyyy-MM-dd')
            ";

        public const string EXIST_STANDARD_TEST_FLAG_SQL = @"
            SELECT CASE WHEN 
            EXISTS (SELECT 1 FROM [TaskScheduling].[TS_StandardTestTasks] WHERE BottomCode = @BottomCode) 
            THEN 1 
            ELSE 0 
            END";

        public const string INSERT_STANDARD_TEST_TASK_SQL = @"
            INSERT INTO
            [TaskScheduling].[TS_StandardTestTasks]
            (
            SampleCode,
            TestCode,
            BottomCode,
            Sampletype,
            TestCnt,
            TaskAssgnTime,
            LastOperTime,
            ExecStas
            )
            VALUES
            (
            @SampleCode,
            @TestCode,
            @BottomCode,
            @Sampletype,
            @TestCnt,
            GETDATE(),
            GETDATE(),
            @ExecStas
            )
            ";

        #endregion

        #region 字段

        private readonly ConnectionStringOption _connectionStrings;

        #endregion

        #region 方法

        public ManualStandardTestTaskWorker(IOptions<ConnectionStringOption> connectionStrings
            ,ILogger<ManualStandardTestTaskWorker> logger)
        {
            _connectionStrings = connectionStrings.Value;
            _logger = logger;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            try
            {
                using (IDbConnection connection = new SqlConnection(_connectionStrings.RL_Web))
                {
                    connection.Open();


                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }

        #endregion
    }
}
